/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cannes.dao.implement;

import cannes.bean.planning.Personne;
import cannes.connexion.ConnexionBDD;
import cannes.dao.DAO;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Pierre
 */
public class PersonneDAO extends DAO<Personne>{
    
    public PersonneDAO(Connection conn) {
        super(conn);
    }

    @Override
    public boolean create(Personne obj) {

        String req = "INSERT INTO personne VALUES ('"+obj.getId()+"',"
                + "'"+obj.getNom()+"','"+obj.getPrenom()+"','"+obj.getPhoto()+ "','" + obj.getNationalite() +"')";
        try {
            ResultSet result = this.connect.createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY).executeQuery(req);
            return result.rowInserted();

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return false;
    }
    

    @Override
    public boolean update(Personne obj) {
        try
        {
        this.connect.createStatement(
                ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY).executeQuery(
                "UPDATE personne SET nom =" + obj.getNom() +",prenom = " + obj.getPrenom() + ",photo" + obj.getPhoto() + ",nationalite =" + obj.getNationalite() + "WHERE personneid = " + obj.getId());
        } catch (SQLException e) {
            e.printStackTrace();

        }
        return false;
    }

    @Override
    public boolean delete(Personne obj) {
        try {
            ResultSet result = this.connect.createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY).executeQuery(
                    "DELETE FROM personne WHERE personneid = " + obj.getId());
            result.rowDeleted();

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return false;
    }

    @Override
    public int next() {
        int i = 0;
        try {
            ResultSet result = this.connect.createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY).executeQuery(
                    "SELECT max(personneid) from personne");
            if (result.first()) i = result.getInt(1)+1;

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }


    @Override
    public Personne find(int id) {
        Personne personne = new Personne();

        try {
            ResultSet result = this.connect.createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY).executeQuery(
                    "SELECT * FROM personne WHERE personneid = " + id);
            if (result.first()) {
                
                personne = new Personne(result.getInt("personneid"),
                        result.getString("nom"), result.getString("prenom"),
                        result.getString("photo"), result.getString("nationalite"));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return personne;

    }

    @Override
    public ArrayList<Personne> findAll() {
        ArrayList<Personne> listePersonne = new ArrayList<Personne>();
        Personne personne;
        
        try {
            ResultSet result = this.connect.createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY).executeQuery(
                    "SELECT * FROM personne");
            while(result.next()) {
                
                personne = new Personne(result.getInt("personneid"),
                        result.getString("nom"), result.getString("prenom"),
                        result.getString("photo"), result.getString("nationalite"));
                listePersonne.add(personne);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return listePersonne;
        
    }
    
}
